package com.agu.mgt.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.agu.entity.sys.SysRoleEntity;
import com.agu.mgt.sys.dao.SysRoleDao;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("sysRoleService")
@Transactional(readOnly = true)
public class SysRoleService {
	@Autowired
	private SysRoleDao sysRoleDao;

	public SysRoleEntity queryObject(Long roleId) {
		return sysRoleDao.queryObject(roleId);
	}

	public List<SysRoleEntity> queryList(Map<String, Object> map) {
		return sysRoleDao.queryList(map);
	}

	public Page<SysRoleEntity> queryPageList(Query query) {
		PageHelper.startPage(query.getPage(), query.getLimit());
		return sysRoleDao.queryPageList(query);
	}

	public int queryTotal(Map<String, Object> map) {
		return sysRoleDao.queryTotal(map);
	}

	@Transactional
	public void save(SysRoleEntity sysRole) {
		
		sysRoleDao.save(sysRole);
		
		Map map = new HashMap();
		
		map.put("roleId", sysRole.getRoleId());
		
		map.put("menuIdList", sysRole.getMenuIdList());
		
		if(sysRole.getMenuIdList().size()>0)
		{
			sysRoleDao.addRolePowers(map);
		}

	}

	@Transactional
	public void update(SysRoleEntity sysRole) {
		
		sysRoleDao.update(sysRole);
		
		sysRoleDao.deleteRolePowers(sysRole.getRoleId());
		
		Map map = new HashMap();
		
		map.put("roleId", sysRole.getRoleId());
		
		map.put("menuIdList", sysRole.getMenuIdList());
		
		if(sysRole.getMenuIdList().size()>0)
		{
			sysRoleDao.addRolePowers(map);
		}

	}

	@Transactional
	public void delete(Long roleId) {
		sysRoleDao.delete(roleId);
	}

	@Transactional
	public void deleteBatch(Long[] roleIds) {
		sysRoleDao.deleteBatch(roleIds);
	}
}
